package com.alibaba.csp.sentinel.dashboard.rule.redis;
 
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @package: com.alibaba.csp.sentinel.dashboard.rule.redis
 * @title: FlowRuleRedisProvider
 * @projectName: sentinel-parent
 * @description: //TODO
 * @author zhuhongqiang3
 * @date:  2020-05-18 10:30
 * @version: V1.0
 * @retrun com.alibaba.csp.sentinel.dashboard.rule.redis.sentinel-parent
 */
@Component("flowRuleRedisProvider")
public class FlowRuleRedisProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
    @Autowired
    private RedisUtil redisConfigUtil;
 
    @Override
    public List<FlowRuleEntity> getRules(String appName) throws Exception {
        String rules = redisConfigUtil.getString(RuleConsts.RULE_FLOW + appName);
        if (StringUtil.isEmpty(rules)) {
            return new ArrayList<>();
        }
        return JSONObject.parseArray(rules,FlowRuleEntity.class);
    }
 
}